<html xmlns:pho="http://www.hitachivantara.com">
<head>
<title>Local Cache Test Harness</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<style  TYPE="text/css">
<!--

.document {
    font-family: Tahoma,Ariel;
    background-color:white;
}

.table {
    border: 1px solid #808080;
}

-->
</style>

</head>

<body class="document">


<h3>Local Cache Tests</h3>

<button onclick="testAll()">Test All</button>

<table id="testtable" border="0">
    <tbody>
    </tbody>
</table>


  <script type="text/javascript" src="webcontext.js?content=common-ui"></script>
  <script type="text/javascript">

    $.extend(djConfig, { modulePaths: {
                            'pentaho.common': "../pentaho/common"
                        },
                        parseOnLoad: true,
                        baseUrl: '../dojo/dojo/'
                    });

  </script>

<script language="javascript" src="../dojo/dojo/dojo-all.js" type="text/javascript"></script>
<script language='javascript' type='text/javascript' src='../cache/LocalCache.js'></script>

<script type="text/javascript">
  dojo.require("dojo.parser");
  dojo.ready(loadPage);

  function loadPage() {
    dojo.parser.parse();

      // create the tests table
      var table = document.getElementById('testtable');

      var idx=1;
      var running = true;
      while( table.rows.length > 1 ) {
          table.deleteRow(1);
      }
      while(running) {
          try {
              eval('test'+idx);
              row = table.insertRow(-1);
              var html = '<td style="height:25px"><button onclick="test('+idx+')">Test '+idx+'</button></td>\n';
              html += '<td style="height:25px;background-color:#ffffff" id="teststatus'+idx+'">---</td>\n';
              html += '<td style="height:25px" id="testdesc'+idx+'">---</td>\n';
              addTableCells( row, html );
          } catch(e) {
              running = false;
          }
          idx++;
      }
  //    test18();
  }

  function addTableCells( rowObj, html ) {
      var tmp = document.createElement("div");
      tmp.innerHTML = '<table><tr>'+html+'</tr></table>';
      tmp = tmp.childNodes[0].childNodes[0].childNodes[0].childNodes;
      while(tmp.length > 0 ) {
          rowObj.appendChild(tmp[0]);
      }
  }


  function reset(idx) {

      document.getElementById('teststatus'+idx).style.backgroundColor="#ffffff";
      document.getElementById('teststatus'+idx).innerHTML="---";
      document.getElementById('testdesc'+idx).innerHTML = "---";

  }

  function testAll() {

      var idx = 1;
      while(document.getElementById('teststatus'+idx) != null) {
          reset(idx);
          idx++;
      }
      idx = 1;
      var running = true;
      while(running) {
          try {
              eval('test('+idx+')');
              idx++;
          } catch (e) {
              running = false;
          }
      }
  }

  function test(idx) {
      try {
          document.getElementById('teststatus'+idx).style.backgroundColor="#ffffff";
          document.getElementById('teststatus'+idx).innerHTML="testing";
          document.getElementById('testdesc'+idx).innerHTML = "";
          eval('test'+idx+'()');
      } catch (e) {
          setResult(idx,'fail',e.message);
      }
  }

  function assertEquals(shouldBe, is, idx, comment) {
      if( shouldBe != is ) {
          setResult(idx,'fail',comment +" - Expected '"+shouldBe+"' but got '"+is+"'");
          return false;
      }
      return true;
  }

  function setResult( idx, status, message ) {
      document.getElementById('teststatus'+idx).innerHTML = status;
      document.getElementById('testdesc'+idx).innerHTML = message;
      document.getElementById('teststatus'+idx).style.backgroundColor=(status=='success') ? "#80ff80" : "#ff8080";
  }

  // test the isAvailable method
  function test1() {
    var available = pentaho.common.localcache.isAvailable();
    if(assertEquals(true, available, 1, 'local caching is not available')) {
      setResult(1, 'success', 'local caching is available');
    }
  }

  // test store & get value methods
  function test2() {
    var dummy = {
      name: "Dummy",
      age: 35
    }
    pentaho.common.localcache.putValue("test", dummy);
    var returned = pentaho.common.localcache.getValue("test");
    if(assertEquals(dummy.name, returned.name, 2, 'Object was not stored/retrieved correctly')){
      if(assertEquals(dummy.age, returned.age, 2, 'Object was not stored/retrieved correctly')) {
        setResult(2, 'success', 'Object was stored & retrieved correctly');
      }
    }
  }

  // test store & get value methods
  function test3() {
    var dummy = {
      name: "Dummy",
      age: 35
    }
    pentaho.common.localcache.putValue("test", dummy, 1234567890);
    var returned = pentaho.common.localcache.getValue("test");
    if(assertEquals(dummy.name, returned.name, 3, 'Object was not stored/retrieved correctly')){
      if(assertEquals(dummy.age, returned.age, 3, 'Object was not stored/retrieved correctly')) {
        setResult(3, 'success', 'Object was stored & retrieved correctly');
      }
    }
  }



  // test clearing the cache
  function test4() {
    pentaho.common.localcache.putValue("test", "test string");
    var returned = pentaho.common.localcache.getValue("test");
    if (returned != null) {
      pentaho.common.localcache.clear("test");
      returned = pentaho.common.localcache.getValue("test");
      if(assertEquals(null, returned, 4, 'Cache was not cleared successfully')) {
        setResult(4, 'success', 'Cache was cleared successfully');
      }
    } else {
      setResult(4, 'fail', 'Could not store a value to test the cache clearing');
    }

  }

  // test setting and getting the last modified timestamp
  function test5() {
    pentaho.common.localcache.putValue("test", {name:"test string", age:35});
    var orig = pentaho.common.localcache.getLastModified("test");

    var now = new Date().getTime();
    pentaho.common.localcache.setLastModified("test", now);
    var returned = pentaho.common.localcache.getLastModified("test");
    if(assertEquals(now, returned, 5, 'Last modified was not stored/retrieved correctly')) {
      if(orig < now) {
        setResult(5, 'success', 'Last modified was stored & retrieved correctly')
      } else {
        setResult(5, 'fail', 'Last modified was stored & retrieved correctly - setting lastModified');
      }
    }
  }

  // test the getCacheKey method
  function test6() {
    var expected = "pentaho_cache_key";
    var dotKey = pentaho.common.localcache.getCacheKey("pentaho.cache.key");
    var dashKey = pentaho.common.localcache.getCacheKey("pentaho-cache-key");
    var mixedKey = pentaho.common.localcache.getCacheKey("pentaho-cache.key");
    var underscoreKey = pentaho.common.localcache.getCacheKey("pentaho_cache_key");

    if(assertEquals(expected, dotKey, 6, 'Dots were not handled properly in the key name')) {
      if(assertEquals(expected, dashKey, 6, 'Dashes were not handled properly in the key name')) {
        if(assertEquals(expected, mixedKey, 6, 'Mixed keys were not handled properly in the key name')) {
          if(assertEquals(expected, underscoreKey, 6, 'Key requiring no change was changed')) {
          setResult(6, 'success', 'Cache keys were escaped properly');
          }
        }
      }
    }

  }

</script>

</body>

</html>
